﻿<template>
  <l-layout class="l-tab-page" v-show="!lr_loadPage">
    <l-panel>
      <template #toolLeft>
        <l-query2
          :items="queryItems"
          :formData="queryData"
          :loading="tableLoading"
          @search="handleSearch"
        >
          <template #f_CategoryId>
            <l-select
              :options="f_CategoryIdOptions"
              v-model="queryData.f_CategoryId"
            />
          </template>
          <template #f_CreateDateQRange>
            <l-date
              format="yyyy-MM-dd"
              dateType="daterange"
              v-model="queryData.f_CreateDateQRange"
              :placeholder="$t('please select')"
            />
          </template>
        </l-query2>
      </template>
      <template #toolRight>
        <l-tool-btns
          :hasTableSetting="true"
          @setting="lr_handleSettingTable"
          :hasAdd="false"
        >
          <l-excel-btns></l-excel-btns>
        </l-tool-btns>
      </template>
      <l-table
        ref="mainTable"
        :loading="tableLoading"
        :columns="lr_getPageColumns(columns)"
        :dataSource="tableData"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
      >
        <template
          v-for="(col, index) in lr_tableColumnsByToLabel(
            lr_getPageColumns(columns)
          )"
          v-slot:[col.prop]="scope"
        >
          <l-value-to-label
            :value="scope.row[col.prop]"
            :type="col.dataType"
            :code="col.dataCode"
            :valueKey="col.valueKey"
            :labelKey="col.labelKey"
            :options="col.options"
            :format="col.format"
            :setting="col.setting"
            :key="index"
          ></l-value-to-label>
        </template>
        <l-table-btns
          :btns="tableBtns"
          @click="lr_handleTableBtnClick"
        ></l-table-btns>
      </l-table>
    </l-panel>

    <!--表单-->
    <l-dialog
      :title="$t(formTitle)"
      :visible.sync="formVisible"
      :height="600"
      :width="800"
      @ok="handleSave"
      @close="handleCloseForm"
      @opened="handleOpenedForm"
    >
      <my-form ref="form" :isReadOnly="formDetails"></my-form>
    </l-dialog>
  </l-layout>
</template>

<script>
import MyForm from "./form";
const api = window.$api.erpcase.log;

export default {
  components: {
    MyForm,
  },
  data() {
    return {
      lr_isPage: true,
      moduleCode: "ERP_Logs",

      // query参数
      queryData: {
        f_CategoryId: "",
        f_CreateDateQRange: "",
      },

      queryItems: [
        { label: "记录类型", prop: "f_CategoryId" },
        { label: "操作时间", prop: "f_CreateDateQRange" },
      ],

      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        {
          label: "记录类型",
          prop: "f_CategoryId",
          minWidth: 120,
          align: "left",
          dataType: "mydata",
          options: [
            { value: "0", label: "客户记录" },
            { value: "1", label: "物料记录" },
            { value: "2", label: "采购记录" },
            { label: "销售记录", value: "3" },
          ],
        },
        {
          label: "关联外键",
          prop: "f_KeyId",
          minWidth: 120,
          align: "left",
          dataType: "input",
          isNotAutoWrap: true,
        },
        {
          label: "记录内容",
          prop: "f_Description",
          minWidth: 200,
          align: "left",
          dataType: "textarea",
          isNotAutoWrap: true,
        },
        {
          label: "操作时间",
          prop: "f_CreateDate",
          minWidth: 120,
          align: "left",
          dataType: "datetime",
          format: "yyyy-MM-dd HH:mm:ss",
        },
        {
          label: "操作人",
          prop: "f_CreateUserId",
          minWidth: 120,
          dataType: "user",
        },
      ],
      tableBtns: [
        // {prop:'Delete',label:'Delete'},
        // {prop:'Details',label:'Details'},
        // {prop:'Edit',label:'Edit'},
      ],

      // 表单参数
      formVisible: false,
      formTitle: "",
      formEditRow: null,
      formEdit: false,
      formDetails: false,
    };
  },
  computed: {
    f_CategoryIdOptions() {
      return [
        { value: "0", label: "客户记录" },
        { value: "1", label: "物料记录" },
        { value: "2", label: "采购记录" },
        { label: "销售记录", value: "3" },
      ];
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loadTableData();
    },

    handleSearch() {
      this.loadTableData();
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = this.$deepClone(this.queryData);

      queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx = "F_CreateDate DESC";
      let data = await this.$awaitWraper(api.getPage(queryData));
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },

    handleDelete($index, row) {
      this.$confirm(
        this.$t("This operation will permanently delete the data. Do you want to continue?"),
        this.$t("Tips"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(async () => {
          this.tableData.splice($index, 1);
          this.tableTotal--;
          await api.remove(row.f_Id);
          this.$message({
            type: "success",
            message: this.$t("Delete  success"),
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("Delete has been canceled!"),
          });
        });
    },
    handleDetails($index, row) {
      this.formEdit = true;
      this.formDetails = true;
      this.formEditRow = row;
      this.handleShowForm("Details");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formDetails = false;
      this.formEditRow = row;
      this.handleShowForm("Edit");
    },

    async handleSave(showLoading, hideLoading) {
      showLoading("saving data...");
      if (await this.$refs.form.validateForm()) {
        const res = await this.$refs.form.saveForm({
          keyValue: this.formEdit ? this.formEditRow.f_Id : "",
          isEdit: this.formEdit,
        });
        if (res) {
          const msg = this.formEdit ? "更新成功!" : "Add成功!";
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.formVisible = false;
        }
      }
      hideLoading();
    },

    handleShowForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
    async handleOpenedForm(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading("Loading data...");
        await this.$refs.form.loadFormData({ keyValue: this.formEditRow.f_Id });
        hideLoading();
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm();
    },
  },
};
</script>
